<template>
<body>
    <div class="login_container">
        <!-- 登录框 -->
        <div class="left">
            <div class="login_box1">
                <!-- logo -->
                <div class="avatar_box">
                    <img src="../assets/logo1.png" alt/>
                </div>  
                <!-- 表单区域 -->
                <el-form ref="loginFormRef" :rules="loginRules" :model="loginForm" class="login_form"  label-width="0">
                    <!-- 用户名 -->
                    <el-form-item prop="username">
                        <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
                    </el-form-item>
                    <!-- 密码 -->
                    <el-form-item prop="password">
                        <el-input v-model="loginForm.password" placeholder="请输入密码"  prefix-icon="el-icon-thumb" type="password" @keyup.enter.native="login()"></el-input>
                    </el-form-item>
                    <!-- 按钮 -->
                    <el-checkbox v-model="checked">记住密码</el-checkbox>
                    <el-form-item class="btns">
                        <el-button type ="success" @click="login()">登录</el-button>
                        <el-button type="info" @click="resetLoginForm()">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="right">
            <div class="login_box2">
                <!-- logo -->
                <div class="avatar_box">
                    <img src="../assets/logo1.png" alt/>
                </div>  
                <!-- 表单区域 -->
                <el-form ref="loginFormRef" :rules="loginRules" :model="loginForm" class="login_form"  label-width="0">
                    <!-- 用户名 -->
                    <el-form-item prop="username">
                        <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
                    </el-form-item>
                    <!-- 密码 -->
                    <el-form-item prop="password">
                        <el-input v-model="loginForm.password" placeholder="请输入密码"  prefix-icon="el-icon-thumb" type="password" @keyup.enter.native="login()"></el-input>
                    </el-form-item>
                    <!-- 按钮 -->
                    <el-checkbox v-model="checked">记住密码</el-checkbox>
                    <el-form-item class="btns">
                        <el-button type ="success" @click="login()">登录</el-button>
                        <el-button type="info" @click="resetLoginForm()">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</body>
</template>
<script>
import { setToken } from '@/utils/auth'
import { loginApi } from '@/api/index'
import { resetRouter } from '@/router/index'
export default {
    data(){
        return{
            checked: true,
            //表单数据对象
            loginForm:{
                username:"",
                password:"",
                note: "0",
            },
            //验证对象
            loginRules:{
                //校验用户名
                username:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },//必填项校验
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }//长度校验
                ],
                //校验密码
                password:[
                    { required: true, message: '请输入密码', trigger: 'blur' },//必填项校验
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }//长度校验
                ],
            },
        };
    },
    methods: {
        resetRouters(){
            resetRouter()
        },
        //重置表单内容
        resetLoginForm(){
            this.$refs.loginFormRef.resetFields();
        },
        login(){
            //1.验证校验规则
            this.$refs.loginFormRef.validate(valid =>{
                if( !valid ) return; 
                loginApi(this.loginForm).then(async res=>{
                    let token = res.data.token
                    await setToken(token)
                    this.$message.success("登陆成功")//信息提示
                    this.$router.push({path:"/home"})//页面跳转
                    window.sessionStorage.setItem("username",this.loginForm.username)
                })
            })
        }
    },
    created() {
        this.resetRouters()
    },
}
</script>
<style lang="less" scoped>
// 根节点样式
.login_container{
    background: url(../assets/bg2.png);
    background-repeat: no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    height: 100%;
}
.login_box{
    width: 400px;
    height: 300px;
    background: rgba(255,255, 255, 0.5);
    -webkit-backdrop-filter: blur();    
    backdrop-filter: blur();
    border-radius: 8px;
    position: absolute;
    left: 50%;
    right: 50%;
    transform: translate(-50%,50%);
    border-block: 20px;
    .avatar_box{
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        border-radius: 50%; //圆角
        padding: 2px;
        box-shadow: 0 0 1px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #eee;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color: #eee;
        }
    }
el-input{
    background: transparent;

}
.btns{
    display: flex;
    justify-content: flex-end;
}
.login_form{
    position: absolute;
    bottom: 0%;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
}
.left{
    width: 50%;
    background-color: red;
}
.right{
    float: left;
    width: 50%;
}
}


</style>